/**
 *
 */
package com.zhuoyue.zhtj.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhuoyue.zhtj.common.ServerResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.common.utils.StringUtils;
import com.zhuoyue.zhtj.entity.Yjfk;
import com.zhuoyue.zhtj.service.YjfkService;

/**
 * 意见反馈Controller
 * @author hss
 * @version 2017-08-15
 */
@Controller
@RequestMapping(value = "${adminPath}/zhtj/yjfk")
public class YjfkController extends BaseController {

	@Autowired
	private YjfkService yjfkService;
	
	@ModelAttribute
	public Yjfk get(@RequestParam(required=false) String id) {
		Yjfk entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = yjfkService.get(id);
		}
		if (entity == null){
			entity = new Yjfk();
		}
		return entity;
	}
	
	@RequiresPermissions("zhtj:yjfk:view")
	@RequestMapping(value = {"list", ""})
	public String list(Yjfk yjfk, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Yjfk> page = yjfkService.findPage(new Page<Yjfk>(request, response), yjfk); 
		model.addAttribute("page", page);
		return "zhuoyue/zhtj/yjfkList";
	}

	@RequiresPermissions("zhtj:yjfk:view")
	@RequestMapping(value = "form")
	public String form(Yjfk yjfk, Model model) {
		model.addAttribute("yjfk", yjfk);
		return "zhuoyue/zhtj/yjfkForm";
	}

	@RequestMapping(value = "frontSave")
	public String frontSave(Yjfk yjfk, HttpServletResponse response){
		if(yjfk.getNr()==null||"".equals(yjfk.getNr())){
			return this.renderString(response, ServerResponse.createByErrorMessage("内容不能为空"));
		}
		yjfkService.save(yjfk);
		return this.renderString(response, ServerResponse.createBySuccessMessage("感谢您的宝贵意见,我们会及时处理"));
	}

	@RequiresPermissions("zhtj:yjfk:edit")
	@RequestMapping(value = "save")
	public String save(Yjfk yjfk, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, yjfk)){
			return form(yjfk, model);
		}
		yjfkService.save(yjfk);
		addMessage(redirectAttributes, "保存意见反馈成功");
		return "redirect:"+Global.getAdminPath()+"/zhtj/yjfk/?repage";
	}
	
	@RequiresPermissions("zhtj:yjfk:edit")
	@RequestMapping(value = "delete")
	public String delete(Yjfk yjfk, RedirectAttributes redirectAttributes) {
		yjfkService.delete(yjfk);
		addMessage(redirectAttributes, "删除意见反馈成功");
		return "redirect:"+Global.getAdminPath()+"/zhtj/yjfk/?repage";
	}
}